package com.snatch.deal.shop.common.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Md5Utils {
    private static final Logger LOGGER = LoggerFactory.getLogger(Md5Utils.class);

    public static byte[] encrypt(byte[] obj) {
        byte[] encodes = null;

        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(obj);
            encodes = md5.digest();
        } catch (NoSuchAlgorithmException var3) {
            LOGGER.warn("encrypt():", var3);
        }

        return encodes;
    }

    public static String encrypt(String str) throws UnsupportedEncodingException {
        return encrypt(str, false);
    }

    public static String encrypt(String str, boolean bUpperCase) throws UnsupportedEncodingException {
        if (str == null) {
            return null;
        } else {
            byte[] byteEncodes = encrypt(str.getBytes("UTF-8"));
            StringBuilder buf = new StringBuilder("");

            for(int offset = 0; offset < byteEncodes.length; ++offset) {
                int i = byteEncodes[offset];
                if (i < 0) {
                    i += 256;
                }

                if (i < 16) {
                    buf.append("0");
                }

                if (bUpperCase) {
                    buf.append(Integer.toHexString(i).toUpperCase());
                } else {
                    buf.append(Integer.toHexString(i));
                }
            }

            return buf.toString();
        }
    }
}
